package com.topisv.tms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.Role;
import com.topisv.tms.entity.Userrole;
import com.topisv.tms.repository.RoleDao;
import com.topisv.tms.repository.UserroleDao;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class RoleService extends BaseService{

	private RoleDao roleDao;
	
	private UserroleDao userroleDao;
	
	public Role  findByRolename(String rolename){
		return roleDao.findByRolename(rolename);
	}

	public Role getRole(Long id) {
		return roleDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void saveRole(Role entity) {
		roleDao.save(entity);
	}
	
	@Transactional(readOnly = false)
	public void saveUserrole(Userrole entity) {
		userroleDao.save(entity);
	}
	
	@Transactional(readOnly = false)
	public void deleteUserrole(Long userid) {
		userroleDao.deleteByUser_id(userid);
	}

	@Transactional(readOnly = false)
	public void deleteRole(Long id) {
		roleDao.delete(id);
	}

	public List<Role> getAllRole() {
		return (List<Role>) roleDao.findAll();
	}

	public Page<Role> getRole(QueryParamHolder paramHolder) {
		return findByJson(paramHolder, roleDao, Role.class);
	}

	@Autowired
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	@Autowired
	public void setUserroleDao(UserroleDao userroleDao) {
		this.userroleDao = userroleDao;
	}
	
}
